package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
类简介：

功能：

时间：2020/4/11

*/
@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {

    @Autowired
    MemberDao memberDao;

    @Autowired
    OrderDao orderDao;

    @Override
    public List<Integer> getMemberCount(ArrayList<String> months) {

        ArrayList<Integer> count = new ArrayList<>();

        for (String month : months) {
            count.add(memberDao.getMemberCountByMonth(month + "-31"));
        }

        return count;
    }

    @Override
    public List<Map<String, Object>> getOrderReport() {
        return orderDao.getReport();
    }

    @Override
    public Map<String, Object> getBusinessReportData() {

        LocalDateTime now = LocalDateTime.now();
        String reportDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        HashMap<String, Object> result = new HashMap<>();

        result.put("reportDate",reportDate);
        result.put("todayNewMember",memberDao.getNewMemberFromAnyDate(now.minusDays(1)));
        result.put("totalMember",memberDao.getTotalMember());
        result.put("thisWeekNewMember",memberDao.getNewMemberFromAnyDate(now.minusDays(now.getDayOfWeek().getValue())));
        result.put("thisMonthNewMember",memberDao.getNewMemberFromAnyDate(now.minusDays(now.getDayOfMonth())));

        result.put("todayOrderNumber",orderDao.getOrderNumberBetweenAnyDate(now.minusDays(1),now.plusDays(1)));
        result.put("todayVisitsNumber",orderDao.getVisitsNumberBetweenAnyDate(now.minusDays(1),now.plusDays(1)));
        result.put("thisWeekOrderNumber",orderDao.getOrderNumberBetweenAnyDate(
                now.minusDays(now.getDayOfWeek().getValue()),now.plusDays(7-now.getDayOfWeek().getValue())
        ));
        result.put("thisWeekVisitsNumber",orderDao.getVisitsNumberBetweenAnyDate(
                now.minusDays(now.getDayOfWeek().getValue()),now.plusDays(7-now.getDayOfWeek().getValue())
        ));
        result.put("thisMonthOrderNumber",orderDao.getOrderNumberBetweenAnyDate(
                now.minusDays(now.getDayOfMonth()),now.minusDays(30-now.getDayOfMonth())
        ));
        result.put("thisMonthVisitsNumber",orderDao.getVisitsNumberBetweenAnyDate(
                now.minusDays(now.getDayOfMonth()),now.minusDays(30-now.getDayOfMonth())
        ));

        result.put("hotSetmeal",orderDao.getHotSetmeal());

        return result;
    }

    @Override
    public Map<String, List<String>> getOrderStatusReport() {

        //统计三个月的数据
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime minusMonths = now.minusMonths(3);

        HashMap<String, List<String>> result = new HashMap<>();

        //拿到有记录的日期
        result.put("days",orderDao.getDays(minusMonths));
        //有记录的日期的预约人数
        result.put("orderCount",orderDao.getOrderCount(minusMonths));
        //有记录的日期的到诊人数
        result.put("visitedCount",orderDao.getVisitedCount(minusMonths));

        return result;
    }

    @Override
    public Map<String, Object> getIncomeReport() {

        HashMap<String, Object> result = new HashMap<>();

        //获得套餐名称和收入
        List<Map<String, String>> data = orderDao.getIncomeReport();
        result.put("data",data);

        ArrayList<String> name = new ArrayList<>();

        for (Map<String, String> map : data) {
            name.add(map.get("name"));
        }

        result.put("name",name);

        return result;
    }
}
